#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("程序员计算器By ch-ch");
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_radioButton_clicked()
{
    //10
    updateBtnState(Base_10);
}

void MainWindow::on_radioButton_2_clicked()
{
    //16
    updateBtnState(Base_16);
}

void MainWindow::on_radioButton_3_clicked()
{
    //2
    updateBtnState(Base_2);
}

void MainWindow::init()
{
    m_currentValue = 0;
    m_firstValue = 0;
    m_index = 0;
    m_timer = new QTimer();
    m_timer->setInterval(500);
    connect(m_timer, &QTimer::timeout, this, [=](){
        //update label
        QString operatorT = operatorList.at(m_customOperator);
        QString text;

        if(m_customOperator != Operator_Init){
            text = QString("公式: %1 %2 %3").arg(m_firstValue).arg(operatorT).arg(m_currentValue);
        }
        else{
            text = QString("公式: %1").arg(m_currentValue);
        }
        ui->label_2->setText(text);
    });
    m_timer->start();

    m_customOperator = Operator_Init;
    ui->lineEdit->setText("0");
    ui->lineEdit_2->setText("0");
    ui->lineEdit_3->setText("0");

    ui->radioButton->setChecked(true);
    updateBtnState(Base_10);
}

void MainWindow::updateBtnState(InputBase base)
{
    m_inputBase = base;

    switch (base) {
    case Base_10:
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_3->setEnabled(true);
        ui->pushButton_4->setEnabled(true);
        ui->pushButton_5->setEnabled(true);
        ui->pushButton_6->setEnabled(true);
        ui->pushButton_7->setEnabled(true);
        ui->pushButton_8->setEnabled(true);
        ui->pushButton_9->setEnabled(true);
        ui->pushButton_10->setEnabled(true);

        ui->pushButton_11->setEnabled(false);
        ui->pushButton_12->setEnabled(false);
        ui->pushButton_13->setEnabled(false);
        ui->pushButton_14->setEnabled(false);
        ui->pushButton_15->setEnabled(false);
        ui->pushButton_16->setEnabled(false);
        break;
    case Base_16:
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_3->setEnabled(true);
        ui->pushButton_4->setEnabled(true);
        ui->pushButton_5->setEnabled(true);
        ui->pushButton_6->setEnabled(true);
        ui->pushButton_7->setEnabled(true);
        ui->pushButton_8->setEnabled(true);
        ui->pushButton_9->setEnabled(true);
        ui->pushButton_10->setEnabled(true);

        ui->pushButton_11->setEnabled(true);
        ui->pushButton_12->setEnabled(true);
        ui->pushButton_13->setEnabled(true);
        ui->pushButton_14->setEnabled(true);
        ui->pushButton_15->setEnabled(true);
        ui->pushButton_16->setEnabled(true);
        break;
    case Base_2:
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_3->setEnabled(false);
        ui->pushButton_4->setEnabled(false);
        ui->pushButton_5->setEnabled(false);
        ui->pushButton_6->setEnabled(false);
        ui->pushButton_7->setEnabled(false);
        ui->pushButton_8->setEnabled(false);
        ui->pushButton_9->setEnabled(false);
        ui->pushButton_10->setEnabled(false);

        ui->pushButton_11->setEnabled(false);
        ui->pushButton_12->setEnabled(false);
        ui->pushButton_13->setEnabled(false);
        ui->pushButton_14->setEnabled(false);
        ui->pushButton_15->setEnabled(false);
        ui->pushButton_16->setEnabled(false);
        break;
    default:
        break;
    }
}

void MainWindow::inputHardKey(QString key)
{
    if(m_inputBase == Base_10){
        ui->lineEdit->setText(ui->lineEdit->text().append(key));
        changeBaseToOtherBase(ui->lineEdit->text());
    }
    else if(m_inputBase == Base_16){
        ui->lineEdit_2->setText(ui->lineEdit_2->text().append(key));
        changeBaseToOtherBase(ui->lineEdit_2->text());
    }
    else{
        ui->lineEdit_3->setText(ui->lineEdit_3->text().append(key));
        changeBaseToOtherBase(ui->lineEdit_3->text());
    }

    QString t1 = ui->lineEdit->text();
    QString t2 = ui->lineEdit_2->text();
    QString t3 = ui->lineEdit_3->text();
    if(t1.startsWith("0")){
        ui->lineEdit->setText(t1.right(t1.length()-1));
    }
    if(t2.startsWith("0")){
        ui->lineEdit_2->setText(t2.right(t2.length()-1));
    }
    if(t3.startsWith("0")){
        ui->lineEdit_3->setText(t3.right(t3.length()-1));
    }
}

void MainWindow::changeBaseToOtherBase(QString value)
{
    if(m_inputBase == Base_10){
        m_currentValue = value.toUInt();

        QByteArray b16 = QByteArray::number(m_currentValue, 16).toUpper();
        QByteArray b2 = QByteArray::number(m_currentValue, 2).toUpper();

        ui->lineEdit_2->setText(QString::fromUtf8(b16));
        ui->lineEdit_3->setText(QString::fromUtf8(b2));
    }
    else if(m_inputBase == Base_16){
        m_currentValue = value.toUInt(nullptr, 16);

        QByteArray b10 = QByteArray::number(m_currentValue, 10).toUpper();
        QByteArray b2 = QByteArray::number(m_currentValue, 2).toUpper();

        ui->lineEdit->setText(QString::fromUtf8(b10));
        ui->lineEdit_3->setText(QString::fromUtf8(b2));
    }
    else{
        m_currentValue = value.toUInt(nullptr, 2);

        QByteArray b10 = QByteArray::number(m_currentValue, 10).toUpper();
        QByteArray b16 = QByteArray::number(m_currentValue, 16).toUpper();

        ui->lineEdit->setText(QString::fromUtf8(b10));
        ui->lineEdit_2->setText(QString::fromUtf8(b16));
    }

    //    qDebug()<<"m_currentValue "<<m_currentValue;
}

void MainWindow::readyForInputSecond()
{
    if(m_customOperator == Operator_Init){
        m_firstValue = m_currentValue;
        m_currentValue = 0;
        //有操作符,需要重新输入数
        ui->lineEdit->setText("0");
        ui->lineEdit_2->setText("0");
        ui->lineEdit_3->setText("0");
    }
}

void MainWindow::on_lineEdit_textEdited(const QString &arg1)
{
    //10
    ui->radioButton->setChecked(true);
    updateBtnState(Base_10);
    changeBaseToOtherBase(arg1);
}

void MainWindow::on_lineEdit_2_textEdited(const QString &arg1)
{
    //16
    ui->radioButton_2->setChecked(true);
    updateBtnState(Base_16);
    changeBaseToOtherBase(arg1);
}

void MainWindow::on_lineEdit_3_textEdited(const QString &arg1)
{
    //2
    ui->radioButton_3->setChecked(true);
    updateBtnState(Base_2);
    changeBaseToOtherBase(arg1);
}

void MainWindow::on_pushButton_clicked()
{
    //0
    inputHardKey("0");
}

void MainWindow::on_pushButton_2_clicked()
{
    //1
    inputHardKey("1");
}

void MainWindow::on_pushButton_3_clicked()
{
    //2
    inputHardKey("2");
}

void MainWindow::on_pushButton_4_clicked()
{
    //3
    inputHardKey("3");
}

void MainWindow::on_pushButton_6_clicked()
{
    //4
    inputHardKey("4");
}

void MainWindow::on_pushButton_7_clicked()
{
    //5
    inputHardKey("5");
}

void MainWindow::on_pushButton_5_clicked()
{
    //6
    inputHardKey("6");
}

void MainWindow::on_pushButton_10_clicked()
{
    //7
    inputHardKey("7");
}

void MainWindow::on_pushButton_9_clicked()
{
    //8
    inputHardKey("8");
}

void MainWindow::on_pushButton_8_clicked()
{
    //9
    inputHardKey("9");
}

void MainWindow::on_pushButton_13_clicked()
{
    //A
    inputHardKey("A");
}

void MainWindow::on_pushButton_14_clicked()
{
    //B
    inputHardKey("B");
}

void MainWindow::on_pushButton_12_clicked()
{
    //C
    inputHardKey("C");
}

void MainWindow::on_pushButton_11_clicked()
{
    //D
    inputHardKey("D");
}

void MainWindow::on_pushButton_15_clicked()
{
    //E
    inputHardKey("E");
}

void MainWindow::on_pushButton_16_clicked()
{
    //F
    inputHardKey("F");
}

void MainWindow::on_pushButton_21_clicked()
{
    //退格
    if(m_inputBase == Base_10){
        QString text = ui->lineEdit->text();
        if(text.length() == 1){
           changeBaseToOtherBase("");
        }
        else if(text.length() > 1){
            ui->lineEdit->setText(text.left(text.length()-1));
            changeBaseToOtherBase(ui->lineEdit->text());
        }
    }
    else if(m_inputBase == Base_16){
        QString text = ui->lineEdit_2->text();
        if(text.length() == 1){
           changeBaseToOtherBase("");
        }
        else if(text.length() > 1){
            ui->lineEdit_2->setText(text.left(text.length()-1));
            changeBaseToOtherBase(ui->lineEdit_2->text());
        }
    }
    else{
        QString text = ui->lineEdit_3->text();
        if(text.length() == 1){
           changeBaseToOtherBase("");
        }
        else if(text.length() > 1){
            ui->lineEdit_3->setText(text.left(text.length()-1));
            changeBaseToOtherBase(ui->lineEdit_3->text());
        }
    }
}

void MainWindow::on_pushButton_22_clicked()
{
    //清屏
    m_customOperator = Operator_Init;
    m_currentValue = m_firstValue = 0;
    ui->lineEdit->setText("0");
    ui->lineEdit_2->setText("0");
    ui->lineEdit_3->setText("0");
}

void MainWindow::on_pushButton_18_clicked()
{
    //+
    readyForInputSecond();
    m_customOperator = Operator_Add;
}

void MainWindow::on_pushButton_17_clicked()
{
    //-
    readyForInputSecond();
    m_customOperator = Operator_Sub;
}

void MainWindow::on_pushButton_20_clicked()
{
    //*
    readyForInputSecond();
    m_customOperator = Operator_Multiply;
}

void MainWindow::on_pushButton_19_clicked()
{
    // /
    readyForInputSecond();
    m_customOperator = Operator_Divide;
}

void MainWindow::on_pushButton_23_clicked()
{
    //=
    switch (m_customOperator) {
    case Operator_Add:
        m_currentValue = m_firstValue + m_currentValue;
        break;
    case Operator_Sub:
        m_currentValue = m_firstValue - m_currentValue;
        break;
    case Operator_Multiply:
        m_currentValue = m_firstValue * m_currentValue;
        break;
    case Operator_Divide:
        if(m_currentValue == 0){
            QMessageBox::information(this, "警告", "除数不能是0!", QMessageBox::Ok);
            return;
        }
        m_currentValue = m_firstValue / m_currentValue;
        break;
    case Operator_Yu:
        m_currentValue = m_firstValue & m_currentValue;
        break;
    case Operator_Huo:
        m_currentValue = m_firstValue | m_currentValue;
        break;
    case Operator_Fei:
        m_currentValue = ~m_firstValue;
        break;
    case Operator_YiHuo:
        m_currentValue = m_firstValue ^ m_currentValue;
        break;
    default:
        break;
    }

    m_customOperator = Operator_Init;

    QByteArray b10 = QByteArray::number(m_currentValue, 10).toUpper();
    QByteArray b16 = QByteArray::number(m_currentValue, 16).toUpper();
    QByteArray b2 = QByteArray::number(m_currentValue, 2).toUpper();

    ui->lineEdit->setText(QString::fromUtf8(b10));
    ui->lineEdit_2->setText(QString::fromUtf8(b16));
    ui->lineEdit_3->setText(QString::fromUtf8(b2));
}

void MainWindow::on_pushButton_24_clicked()
{
    //与
    readyForInputSecond();
    m_customOperator = Operator_Yu;
}

void MainWindow::on_pushButton_25_clicked()
{
    //或
    readyForInputSecond();
    m_customOperator = Operator_Huo;
}

void MainWindow::on_pushButton_26_clicked()
{
    //非
    readyForInputSecond();
    m_customOperator = Operator_Fei;
    on_pushButton_23_clicked();
}

void MainWindow::on_pushButton_27_clicked()
{
    //异或
    readyForInputSecond();
    m_customOperator = Operator_YiHuo;
}
